import javax.naming.InitialContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@SuppressWarnings("Duplicates")
@WebServlet(urlPatterns = {"/The"})
public class Score extends HttpServlet {
    protected void doPost (HttpServletRequest req, HttpServletResponse resp)throws IOException
    {
        try{
            InitialContext context=new InitialContext();
            DataSource dataSource=(DataSource)context.lookup("jdbc/DSTest");
            Connection connection=null;
            Statement statement=null;
            ResultSet resultSet=null;
            connection=dataSource.getConnection();
            statement=connection.createStatement();
            statement.executeUpdate(
                    "CREATE TABLE IF NOT EXISTS `Score` (" +
                            "  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,"+
                            "  `no` int(11) unsigned NOT NULL," +
                            "  `kemu` varchar(100) NOT NULL," +
                            "  `score` int(11) NOT NULL," +
                            "  PRIMARY KEY (`id`)" +
                            ");"
            );
            statement.executeUpdate(
                    "insert into `Score` (`no`,`kemu`,`score`) values (01, 'C', 88);" +
                            "insert into `Score` (`no`,`kemu`,`score`) values (04, 'JAVA', 87);"+
                            "insert into `Score` (`no`,`kemu`,`score`) values (04, 'Python', 86);"
            );
            resp.setContentType("text/html;charset=UTF-8");
            resultSet=statement.executeQuery("select * from Score");
            while(resultSet.next())
            {
                resp.getWriter().println("学号:"+resultSet.getInt("no")+"  科目:"+resultSet.getString("kemu")+"  成绩:"+resultSet.getInt("score")+"<br>");
            }
        }
        catch (Exception e){
            System.out.println(e);
        }
    }
    protected void doGet (HttpServletRequest req, HttpServletResponse resp)throws IOException
    {
        doPost(req,resp);
    }
}
